<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const store = testHelpers.createStore({}, ({ dispatch }) => next => action => {
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            dispatch({
              type: 'WEB_CHAT/SET_NOTIFICATION',
              payload: {
                id: 'toast1',
                level: 'info',
                message: 'This is first toast.'
              }
            });

            dispatch({
              type: 'WEB_CHAT/SET_NOTIFICATION',
              payload: {
                id: 'toast2',
                level: 'info',
                message: 'This is second toast.'
              }
            });
          } else if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
            dispatch({
              type: 'WEB_CHAT/SET_NOTIFICATION',
              payload: {
                id: 'toast3',
                level: 'info',
                message: `This is third toast at ${Math.random().toString(10).substr(2, 3)}.`
              }
            });
          }

          return next(action);
        });

        WebChat.renderWebChat(
          {
            directLine: WebChat.createDirectLine({
              token: await testHelpers.token.fetchDirectLineToken()
            }),
            store
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();
        await pageConditions.toastShown();

        await pageObjects.verifyDOMIntegrity();

        await pageObjects.clickToasterHeader();

        // After click on the toaster header, it will expand and two toasts will become visible.

        await pageObjects.verifyDOMIntegrity();

        await pageObjects.dismissToast(0);

        // One toast left visible on the screen.

        await pageObjects.verifyDOMIntegrity();

        await pageObjects.dismissToast(0);

        // All toasts have been dismissed.

        await pageObjects.verifyDOMIntegrity();
      });
    </script>
  </body>
</html>
